package com.app.couponsharelife.ui.adapter
import android.content.Context
import android.content.Intent
import android.text.Html
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.app.couponsharelife.R
import com.app.couponsharelife.data.bean.LifeShopRecord
import com.app.couponsharelife.data.bean.TradingArea
import com.app.couponsharelife.ui.main.nativelife.activity.NativeLifeDetailsActivity
import com.app.couponsharelife.utils.LoadUtils
import com.app.couponsharelife.utils.ToastUtil
import kotlinx.android.synthetic.main.lifesquare_baservitm.view.*
import kotlinx.android.synthetic.main.lifesquare_baservtopitm.view.*

/**
 *Created by xct on 2019/1/9
 *Description:
 */
class LifeSquareBaseAdapter(private var context: Context,private var dats:List<LifeShopRecord>,private var mTradingArea: TradingArea):RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    private val TYPETOP=0
    private val TYPEITEM=1
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return if (TYPETOP==viewType){
            val view=LayoutInflater.from(context).inflate(R.layout.lifesquare_baservtopitm,parent,false)
            MyTopHolder(view)
        }else{
            val view=LayoutInflater.from(context).inflate(R.layout.lifesquare_baservitm,parent,false)
            MyHolder(view)
        }
    }

    override fun getItemCount(): Int {
        return if (dats.isNotEmpty()){
            dats.size+1
        }else{
            0
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is MyTopHolder){
            holder.tvAddress.text=mTradingArea.address
        }else if(holder is MyHolder){
            if (dats.isNotEmpty()){
                val lifeCardBean = dats[position-1]
                holder.tvName.text=lifeCardBean.shopName?:""
                holder.tvClassName.text=lifeCardBean.className?:""
                holder.tvJL.text=""
                LoadUtils().loadImag(context,holder.imageLogo, lifeCardBean.localImage?:"", R.drawable.ic_tribe_boy_thumb)
                val imagUrl = if (lifeCardBean.images!=null&&lifeCardBean.images.isNotEmpty()){
                    val imags=lifeCardBean.images.split(",")
                    imags[0]
                }else{
                    lifeCardBean.images?:""
                }
                LoadUtils().loadImag(context,holder.imageView, imagUrl, R.drawable.lifereferrals_bannenr)
                if (lifeCardBean.activity!=null){
                    holder.tvTitle.text= Html.fromHtml("<font color=\"#9000FC\">#热门活动#</font>" + lifeCardBean.activity!!.introduct)
                }
                if (lifeCardBean.maxReduceMoney!=null&&lifeCardBean.maxReduceMoney!!>0){
                    holder.tvPrice.text="￥"+lifeCardBean.maxReduceMoney
                    holder.tvPrice.visibility=View.VISIBLE
                }else{
                    holder.tvPrice.visibility=View.GONE
                }
                if (lifeCardBean.minDiscount!=null&&lifeCardBean.minDiscount!!>0){
                    holder.tvZK.text=lifeCardBean.minDiscount.toString()+"折"
                    holder.tvZK.visibility=View.VISIBLE
                }else{
                    holder.tvZK.visibility=View.GONE
                }
                if (lifeCardBean.giftNum!=null&&lifeCardBean.giftNum!!>0){
                    holder.tvFlName.visibility=View.VISIBLE
                }else{
                    holder.tvFlName.visibility=View.GONE
                }
                holder.itemView.setOnClickListener {
                    if (lifeCardBean.id.isEmpty()){
                        ToastUtil.getInstance().showToast("数据出错请稍后再试！")
                    }else {
                        val intent = Intent(context, NativeLifeDetailsActivity::class.java)
                        intent.putExtra("ShopID", lifeCardBean.id)
                        context.startActivity(intent)
                    }
                }
            }
        }
    }

    override fun getItemViewType(position: Int): Int {
        return if (position==0){
            TYPETOP
        }else{
            TYPEITEM
        }
    }
    inner class MyTopHolder(itemView:View):RecyclerView.ViewHolder(itemView){
        var tvAddress=itemView.lifesquare1_topitm_dw
    }
    inner class MyHolder(itemView:View):RecyclerView.ViewHolder(itemView){
        var imageView=itemView.lifereferrals_rvitem_imag
        var imageLogo=itemView.lifereferrals_rvitem_logo
        var tvName=itemView.lifereferrals_rvitem_shopname
        var tvTitle=itemView.lifereferrals_rvitem_title
        var tvZK=itemView.lifereferrals_rvitem_zk
        var tvClassName=itemView.lifereferrals_rvitem_classname
        var tvJL=itemView.lifereferrals_rvitem_jl
        var tvPrice=itemView.lifereferrals_rvitem_price
        var tvFlName=itemView.lifereferrals_rvitem_flname
    }
}